package br.com.sistema.vendas.DAO;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;

import br.com.sistema.vendas.DAOLocal.GenericoDAOLocal;

public class GenericoDAO<T> implements GenericoDAOLocal<T>, Serializable {

	private static final long serialVersionUID = 1L;
	private Class<T> classe;
	private Session sessao;

	public GenericoDAO(Class<T> classe, Session sessao) {
		super();
		this.classe= classe;
		this.sessao= sessao;
	}
	public Session getSessao() {
		return sessao;
	}

	public void setSessao(Session sessao) {
		this.sessao = sessao;
	}

	@Override
	public void save(T entity) {
		this.sessao.save(entity);
		
	}

	@Override
	public void remove(T entity) {
		this.sessao.delete(entity);
		
	}

	@Override
	public void update(T entity) {
		this.sessao.update(entity);
	}

	@Override
	public void merge(T entity) {
		this.sessao.merge(entity);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public T getEntity(Serializable id) {
		T entity = (T) sessao.get(classe, id);
		return entity;
	}

	@SuppressWarnings("unchecked")
	@Override
	public T getEntityByDetachedCriteria(org.hibernate.criterion.DetachedCriteria criteria) {
		T entity = (T) criteria.getExecutableCriteria(sessao).uniqueResult();
		return entity;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<T> getEntities() {
		List<T> entities = (List<T>) sessao.createCriteria(classe).list();
		return entities;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> DetachedCriteria(org.hibernate.criterion.DetachedCriteria criteria) {
		return criteria.getExecutableCriteria(sessao).list();
	}
	
}
